
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  Registration;

import java.io.RandomAccessFile;
import java.sql.ResultSet;

import logger.PoolLogger;
import User.Uservalidation;

import control.Control;
import control.SmsControl;
import dbmanager.DBManager;

public class RegisterRate 
{
		String Path;
		PoolLogger pl;
		
		public RegisterRate(String Path) 
		{		
			this.Path = Path;
			// logger
			pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
		}
		public int Register()
		{
			int Count=0;
			
			try 
			{
				RandomAccessFile raf = new RandomAccessFile(Path,"rws");
				RandomAccessFile log = new RandomAccessFile("//home//ubuntu//new pricelist//Request.txt","rws");
				RandomAccessFile Error = new RandomAccessFile("//home//ubuntu//new pricelist///Error.txt","rws");
				
				String line = null;
				line=raf.readLine();
				String[] records = line.split("\t");
				String col[] = new String[8]; 
				col[0] = records[0];	//Item
				col[1] = records[1];	//Unit
				col[2] = records[2];	//Usergroup
				col[3] = records[3];	//Buy
				col[4] = records[4];	//Sell
				col[5] = records[5];	//MRP
				//col[6] = records[6];     //Vat
				//col[7] = records[7];	//Discount
				
				ResultSet rs = DBManager.getSelect("SELECT * FROM command where lower(fieldname)='"+col[0]+"' and msgtype = 'rate_master'");
				rs.next();
				col[0] = rs.getString("spchar");
				
				rs = DBManager.getSelect("SELECT * FROM command where lower(fieldname)='"+col[1]+"' and msgtype = 'rate_master'");
				rs.next();
				col[1] = rs.getString("spchar");
				
				rs = DBManager.getSelect("SELECT * FROM command where lower(fieldname)='"+col[2]+"' and msgtype = 'rate_master'");
				rs.next();
				col[2] = rs.getString("spchar");
			
				rs = DBManager.getSelect("SELECT * FROM command where lower(fieldname)='rate' and msgtype = 'rate_master'");
				rs.next();
				String rateChar = rs.getString("spchar");
				
				rs = DBManager.getSelect("SELECT * FROM command where lower(fieldname)='buysell' and msgtype = 'rate_master'");
				rs.next();
				String buysellChar = rs.getString("spchar");
					
				String userid = "+911234567891";
				Uservalidation getpass = new Uservalidation();
				String password = getpass.getPasword(userid);
				
				while((line=raf.readLine())!=null)
				{
					String message ="";
					try 
					{
						String propString = "";
						records = line.split("\t");

						for (int i = 0; i < 3; i++) 
						{
							try 
							{
								String pv = records[i].trim();
								if (pv.length() == 0)
									pv = "0";
								propString = propString + col[i].trim()+ "\"" + pv + "\"";
							} catch (Exception e)
							{
								String pv = "0";
								propString = propString + col[i].trim()+ "\"" + pv + "\"";
							}
						}
					
						for (int j = 3; j < 8; j++) 
						{	
							String temp;
							try
							{
								String pv = records[j].trim();
								if (pv.length() != 0)
								{
									message = "11 rate_master add"+propString+rateChar+"\""+pv+"\""+buysellChar+"\""+col[j].trim()+"\""+"#"+password;
									SmsControl controlClassObj = new SmsControl(userid,message);
									controlClassObj.messageProcessing();
									temp = controlClassObj.requestProcess().toString();
									if(temp.contains("error"))
										Error.writeBytes(message+"\n");
							  }
							}
							catch (Exception e) {
								Error.writeBytes(message+"\n");
							}
							System.out.println("MESSGE IS :"+message);
							pl.info("MESSGE IS :"+message);
							pl.debug("MESSGE IS :"+message);
							pl.warn("MESSGE IS :"+message);
							pl.fatal("MESSGE IS :"+message);
							pl.error("MESSGE IS :"+message);
							pl.trace("MESSGE IS :"+message);
						}						
						Count++;
					} 
					catch (Exception e) 
					{
						Error.writeBytes(message+"\n");
					}
				}
				log.close();
				Error.close();
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			
			return Count;
		}
		public static void main(String[] args) 
		{
			RegisterRate reg = new RegisterRate("//home//ubuntu//new pricelist//Newpricelist.csv");
			System.out.println("RAte Counter :"+reg.Register());
		}
}